<?php
/**
 * Created by IntelliJ IDEA.
 * User: fang.cai.liang@aliyun.com
 * Date: 2017/7/4
 * Time: 15:27
 */

namespace nflow;


/**
 * Class Factory
 * @package nflow
 */
class Factory
{

    /**
     * @var array
     */
    private static $instanceList = [];

    /**
     * 返回一个 API 供应商的实例
     * @param $providerName
     * @param $conf
     * @return mixed
     * @throws \Exception
     */
    public static function getInstance($providerName, $conf){
        if(!empty($providerName)){
            if(isset(self::$instanceList[$providerName])){
                return self::$instanceList[$providerName];
            }else{
                self::$instanceList[$providerName] = self::create($providerName, $conf);
                return self::$instanceList[$providerName];
            }
        }
        throw new \Exception('provider 名为空!', 11002);
    }

    /**
     * 创建 API 供应商的实例
     * @param $providerName
     * @param $conf
     * @return mixed
     * @throws \Exception
     */
    public static function create($providerName, $conf){
        $instance = '\nflow\\'.$providerName;
        if($conf && is_array($conf)){
            $operInstance = new $instance($conf);
            if($operInstance instanceof AOperator){
                return $operInstance;
            }else{
                throw new \Exception($providerName.' 不是一个有效的 Operator ！', 11001);
            }
        }
        throw new \Exception($providerName.' 的配置信息无效！', 11003);
    }
 
    
}